# -*- coding: utf-8 -*-
from datetime import datetime

from app import db


collections = db.Table('collections',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('post_id', db.Integer, db.ForeignKey('post.id')),
    db.Column('timestamp', db.DateTime, default=datetime.now())
)


class Post(db.Model):
    __tablename__ = 'post'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255))
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    author = db.relationship('User',
        backref=db.backref('post', lazy='dynamic'))
    pub_date = db.Column(db.DateTime, default=datetime.now())
    update_time = db.Column(db.DateTime, default=datetime.now())
    views = db.Column(db.Integer, default=0)
    good = db.Column(db.Integer, default=0)
    comment_count = db.Column(db.Integer, default=0)
    content_source = db.Column(db.Text)
    content = db.Column(db.Text)
    collected_count = db.Column(db.Integer, default=0)
    collected_by = db.relationship('User', secondary=collections,
        backref=db.backref('collected_posts', lazy='dynamic'))


class Comment(db.Model):
    __tablename__ = 'comment'

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(500))
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    author = db.relationship('User',
        backref=db.backref('comment', lazy='dynamic'))
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
    post = db.relationship('Post',
        backref=db.backref('comments', lazy='dynamic'))
    pub_date = db.Column(db.DateTime, default=datetime.now())
    good = db.Column(db.Integer, default=0)

